package com.skynet.springcloud.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.skynet.springcloud.entities.Dept;
import com.skynet.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController_Provider {

    @Autowired
    private DeptService deptService;
    @Autowired
    private DiscoveryClient client;

    @GetMapping("/dept/list")
    public List<Dept> list(){
        return deptService.queryAll();
    }

    @RequestMapping(value="/dept/{id}",method=RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id){
    	Dept dept=this.deptService.queryById(id);
    	if(null==dept){
    		throw new RuntimeException("查不到信息"+id);
    	}
        return dept;
    }

    @PostMapping("/dept/add")
    public boolean add(@RequestBody Dept dept) {
        return deptService.addDept(dept);
    }
    
    @RequestMapping(value="/dept/discover",method=RequestMethod.GET)
    public Object discover(){
    	List<String> ls=client.getServices();
    	System.out.println(ls);
    	List<ServiceInstance> lsi=client.getInstances("MICROSERVICE-PROVIDER");
    	for(ServiceInstance si:lsi){
    		System.out.println("serviceinstance@@@@@@@@@@@@@@@@"+si.getHost());
    		System.out.println(si.getPort());
    		System.out.println(si.getServiceId());
    		System.out.println(si.getUri());
    	}
    	return this.client;
    }

}
